﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Dashboard.ClusteringSvc;

namespace Dashboard
{
    public partial class TermGrid : UserControl
    {
        public event OnSwitchToClusterTopView SwitchToClusterTopView;
        private Dictionary<int, Dictionary<int,TermDetail>> termDetails;

        public TermGrid()
        {
            InitializeComponent();
        }

        public void LoadTermGridResult(DocumentClusteringOutput output)
        {
            this.termDetails = new Dictionary<int, Dictionary<int, TermDetail>>();
            Dictionary<int,TermDetail> termDetailsForAllClusters=new Dictionary<int, TermDetail>();
            foreach(int clusterID in output.ClusteredDocs.Keys)
            {
                Dictionary<int,TermDetail> termDetailsForCluster=new Dictionary<int, TermDetail>();
                foreach(int docID in output.ClusteredDocs[clusterID])
                {
                    foreach(int termID in output.DocTermFreqs[docID].Keys)
                    {
                        if(termDetailsForCluster.ContainsKey(termID))
                        {
                            TermDetail termDetail = termDetailsForCluster[termID];
                            termDetail.Docs++;
                            termDetail.Freq += output.DocTermFreqs[docID][termID];
                        }
                        else
                        {
                            TermDetail termDetail = new TermDetail()
                                                        {
                                                            Docs = 1,
                                                            Freq = output.DocTermFreqs[docID][termID],
                                                            TermID = termID,
                                                            Weight = output.TermWeights[termID],
                                                            Word = output.AllTerms[termID]
                                                        };
                            termDetailsForCluster.Add(termID,termDetail);
                        }
                        if (termDetailsForAllClusters.ContainsKey(termID))
                        {
                            TermDetail termDetail = termDetailsForCluster[termID];
                            termDetail.Docs++;
                            termDetail.Freq += output.DocTermFreqs[docID][termID];
                        }
                        else
                        {
                            TermDetail termDetail = new TermDetail()
                            {
                                Docs = 1,
                                Freq = output.DocTermFreqs[docID][termID],
                                TermID = termID,
                                Weight = output.TermWeights[termID],
                                Word = output.AllTerms[termID]
                            };
                            termDetailsForAllClusters.Add(termID, termDetail);
                        }
                    }
                }
                this.termDetails.Add(clusterID, termDetailsForCluster);
            }
            this.termDetails.Add(0, termDetailsForAllClusters);

            this.dgTermGrid.ItemsSource = termDetailsForAllClusters.Values;
        }

        public void LoadTermGridResultForCluster(int clusterID)
        {
            if(this.termDetails !=null && this.termDetails.ContainsKey(clusterID))
            {
                this.dgTermGrid.ItemsSource = this.termDetails[clusterID].Values;
                this.txtAllCluster.TextDecorations = TextDecorations.Underline;
                this.txtSingleCluster.Visibility = Visibility.Visible;
                this.txtSingleCluster.Text = "Cluster #" + clusterID.ToString();
            }
        }

        private void txtAllCluster_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.txtAllCluster.TextDecorations = null;
            this.txtSingleCluster.Visibility = System.Windows.Visibility.Collapsed;
            if (this.termDetails != null && this.termDetails.ContainsKey(0))
                this.dgTermGrid.ItemsSource = this.termDetails[0].Values;
            if (this.SwitchToClusterTopView != null)
                this.SwitchToClusterTopView();
        }

    }

    public class TermDetail
    {
        public int TermID { get; set; }
        public string Word { get; set; }
        public int Freq { get; set; }
        public int Docs { get; set; }
        public double Weight { get; set; }
    }
}
